package splug.cjk;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import splug.cjk.packet.BBSPacket;

public class BBSNoticeRead extends Thread{
	
	private OutputStream os;
	
	public BBSNoticeRead(OutputStream os){
		this.os = os;
	}
	
	public void run(){
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/carpool?useUnicode=true&characterEncoding=euckr","root","apmsetup");
			Statement stmt = conn.createStatement();

			String query1 = "select title, id, content, date from bbsnotice order by number desc";

			stmt.execute("SET CHARACTER SET euckr");
			stmt.execute("set names euckr");

			ResultSet rs1 = stmt.executeQuery(query1);
			
			ArrayList<Content> content = new ArrayList<Content>();
			ArrayList<Content> sub_content = new ArrayList<Content>();
			

			while(rs1.next()){
				String str1 = rs1.getString("title");
				String str2 = rs1.getString("id");
				String str3 = rs1.getString("content");
				String str4 = rs1.getString("date");
				Content c = new Content(str1, str2);
				content.add(c);
				c = new Content(str3, str4);
				sub_content.add(c);
			}
			
			BBSPacket packet = new BBSPacket(content, sub_content,0,"","","","");
			ObjectOutputStream output = new ObjectOutputStream(os);
			output.writeObject(packet);
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
